package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.user.entities.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import com.atguigu.gmall.user.vo.UserLoginSuccessVo;
import com.atguigu.gmall.user.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName LoginSuccessApi
 * @Description TODO
 * @Author king
 * @Date 2022/11/14 20:13
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/user")
public class LoginSuccessApi {

    @Autowired
    UserInfoService userInfoService;

    @PostMapping("/passport/login")
    public Result loginSuccess(@RequestBody UserLoginVo userLoginVo, HttpServletRequest request){

        UserLoginSuccessVo vo = userInfoService.login(userLoginVo, request);

        if (vo == null){
            return Result.build(null, ResultCodeEnum.LOGIN_INVAILD);
        }
        return Result.ok(vo);

    }

    @GetMapping("/passport/logout")
    public Result loginOut(@RequestHeader("token") String token){
        userInfoService.loginOut(token);
        return Result.ok();
    }

}
